﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace HandRise.MBHOP.Controls
{
    public class DataListItem : Control
    {
        #region 属性


        private Font font = new Font("雅黑", 12, FontStyle.Regular);
        /// <summary>
        /// 文本的字体

        /// </summary>
        public override Font Font
        {
            get
            {
                return font;
            }
            set
            {
                font = value;
            }
        }

        private string text = "";
        /// <summary>
        /// 显示的文本

        /// </summary>
        public override string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        private Color backColor = Color.White;
        /// <summary>
        /// 背景色

        /// </summary>
        public override Color BackColor
        {
            get
            {
                return backColor;
            }
            set
            {
                if (value != backColor)
                {
                    backColor = value;
                    exitsIcon = false;
                }
            }
        }

        int leftSpace = 0;
        /// <summary>
        /// 文本的左边距
        /// </summary>
        public int LeftSpace
        {
            get
            {
                return leftSpace;
            }
            set
            {
                leftSpace = value;
            }
        }

        int rightSpace = 0;
        /// <summary>
        /// 文本的右边距
        /// </summary>
        public int RightSpace
        {
            get
            {
                return rightSpace;
            }
            set
            {
                rightSpace = value;
            }
        }

        int iconWidth = 0;
        /// <summary>
        /// 图标框长度

        /// </summary>
        public int IconWidth
        {
            get
            {
                return iconWidth;
            }
            set
            {
                iconWidth = value;
            }
        }

        internal ImageList itemsIconList = new ImageList();
        /// <summary>
        /// 图标列表
        /// </summary>
        internal ImageList ItemsIconList
        {
            get
            {
                return itemsIconList;
            }
            set
            {
                itemsIconList = value;
            }
        }

        private int iconIndex = 0;
        /// <summary>
        /// 图标的索引

        /// </summary>
        public int IconIndex
        {
            get
            {
                return iconIndex;
            }
            set
            {
                if (iconIndex != value)
                {
                    exitsIcon = false;
                    iconIndex = value;
                }
            }
        }

        internal object dataSource = null;
        /// <summary>
        /// 图标的数据源
        /// </summary>
        public object DataSource
        {
            get
            {
                return dataSource;
            }
        }



        internal ButtonStateEnum itemSelectState = ButtonStateEnum.Up;
        /// <summary>
        /// 图片的按下和弹起状态

        /// </summary>
        internal ButtonStateEnum ItemSelectState
        {
            get
            {
                return itemSelectState;
            }
            set
            {
                itemSelectState = value;
            }
        }

        internal bool textCenter = false;
        /// <summary>
        /// 文本是否居中
        /// </summary>
        internal bool TextCenter
        {
            get
            {
                return textCenter;
            }
            set
            {
                textCenter = value;
            }
        }

        //文字的显示面积

        private SizeF textSize = new SizeF();

        //纪录图标
        internal DataListItemIconList iconList = null;

        /// <summary>
        /// 是否已经画图标

        /// </summary>
        internal bool exitsIcon = false;

        /// <summary>
        /// 项创建事件

        /// </summary>
        //internal event EventItemCreateBefore OnItemCreateBefore;
        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //用于弹出框区域空白,当刷新区域跨图标区域和文字区域时才全部刷新，否则只刷新相应的区域
            bool drawIcon = false;
            bool drawText = false;
            if (e.ClipRectangle.X < IconWidth)
            {
                drawIcon = true;
                if (e.ClipRectangle.Width > IconWidth - e.ClipRectangle.X)
                    drawText = true;
            }
            else
                drawText = true;



            if ((!exitsIcon && IconWidth > 0 && ItemsIconList != null && ItemsIconList.Images.Count > 0 && iconIndex < ItemsIconList.Images.Count && iconList != null) || drawIcon)
            {
                try
                {
                    //计算图标的位置

                    int IconX = 0;
                    int IconY = 0;
                    if (ItemsIconList.Images[iconIndex].Width < IconWidth)
                    {
                        IconX = (IconWidth - ItemsIconList.Images[iconIndex].Width) / 2;
                    }
                    if (ItemsIconList.Images[iconIndex].Height < this.Height)
                    {
                        IconY = (this.Height - ItemsIconList.Images[iconIndex].Height) / 2;
                    }

                    //画图标

                    if (iconList[itemSelectState, iconIndex] == null)
                    {

                        Bitmap bit = new Bitmap(ItemsIconList.Images[iconIndex].Width, ItemsIconList.Images[iconIndex].Height);
                        Color startColor = ((Bitmap)(ItemsIconList.Images[iconIndex])).GetPixel(0, 0);
                        Graphics g = Graphics.FromImage(bit);
                        g.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, bit.Width, bit.Height));
                        ImageAttributes imageAttr = new ImageAttributes();
                        imageAttr.SetColorKey(startColor, startColor);
                        g.DrawImage(ItemsIconList.Images[iconIndex], new Rectangle(0, 0, bit.Width, bit.Height),
                            0, 0, bit.Width, bit.Height, GraphicsUnit.Pixel, imageAttr);
                        iconList.Add(bit, itemSelectState, iconIndex);
                        g.Dispose();
                    }
                    e.Graphics.DrawImage(iconList[itemSelectState, iconIndex],
                    new Rectangle(IconX, IconY, IconWidth, this.Height),
                    new Rectangle(0, 0, IconWidth, this.Height), GraphicsUnit.Pixel);
                    exitsIcon = true;
                }
                catch { }
            }
            else
            {
                exitsIcon = false;
            }

            if (((e.ClipRectangle == this.ClientRectangle || e.ClipRectangle.X == this.iconWidth) && Text != "") || drawText)
            {
                //计算要画的文本

                string drawStr = GetShowString(e.Graphics);
                //计算居中上边距

                int TextY = 0;
                if (textSize.Height < this.Height)
                {
                    TextY = (this.Height - (int)textSize.Height) / 2;
                }
                if (!textCenter)
                {
                    if (font.Size > 1)
                        e.Graphics.DrawString(drawStr, font, new SolidBrush(this.ForeColor), new RectangleF(IconWidth + leftSpace, TextY, this.Width - iconWidth - rightSpace - leftSpace, textSize.Height));
                }
                else
                {
                    int TextX = 0;
                    if (textSize.Width < this.Width)
                        TextX = (this.Width - (int)textSize.Width) / 2;
                    e.Graphics.DrawString(drawStr, font, new SolidBrush(this.ForeColor), new RectangleF(TextX, TextY, this.Width - iconWidth - rightSpace - leftSpace, textSize.Height));
                }
            }
        }

        private string GetShowString(Graphics g)
        {

            SizeF size = new SizeF();
            StringBuilder sb = new StringBuilder(this.Text);
            size = g.MeasureString(sb.ToString(), font);
            if (size.Width > this.Width - iconWidth - rightSpace - leftSpace)
            {
                for (int i = 0; i < sb.Length; i++)
                {

                    size = g.MeasureString(sb.ToString(0, i) + "…", font);

                    if (size.Width > this.Width - iconWidth - rightSpace - leftSpace - Font.Size)
                    {
                        textSize = size;
                        return sb.ToString(0, i) + "…";
                    }
                }
            }
            textSize = size;
            return sb.ToString();
        }


    }
}
